//
//  CategoriesTableView.m
//  AFNetworking
//
//  Created by admin on 2020/4/28.
//

#import "CategoriesTableView.h"

@implementation CategoriesTableView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self confitUI];
    }
    return self;
}

-(void)confitUI{
    [self addSubview:self.tableView];
}


-(void)setDatalist:(NSMutableArray *)datalist{
    _datalist = datalist;
    _selectData = _datalist.firstObject;
    [_tableView reloadData];
}

-(void)setSelectData:(NSDictionary *)selectData{
    if (_selectData != selectData) {
        _selectData = selectData;
        [_tableView reloadData];
    }
}

-(UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = UIColor.clearColor;
        [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        [_tableView setTableFooterView:[UIView new]];
    }
    return _tableView;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.datalist.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* indentifier = @"cell";
    UITableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    NSDictionary *dic = [_datalist objectAtIndex:indexPath.row];
    
    cell.textLabel.text = [dic stringValueForKey:@"title" default:@""];
    cell.textLabel.font = FONT_SYS13;
    if ([dic isEqual:_selectData]) {
        cell.backgroundColor = UIColor.whiteColor;
        cell.textLabel.textColor = COLOR_33;
    }else{
        cell.backgroundColor = COLOR_F5;
        cell.textLabel.textColor = COLOR_66;
        
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = [_datalist objectAtIndex:indexPath.row];
    [self setSelectData:dic];
    if (_cellClickBlock) {
        _cellClickBlock(indexPath);
    }
}


-(void)layoutSubviews{
    [super layoutSubviews];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self);
        make.right.mas_equalTo(self);
        make.top.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}

@end
